package com.chen.blog.config;

import com.chen.blog.common.Const;
import com.chen.blog.common.ServerResponse;
import com.chen.blog.pojo.Setting;
import com.chen.blog.pojo.viewconfig.ViewAboutStation;
import com.chen.blog.pojo.viewconfig.ViewCarousel;
import com.chen.blog.pojo.viewconfig.ViewConfiguration;
import com.chen.blog.pojo.viewconfig.ViewContactMe;
import com.chen.blog.pojo.viewconfig.ViewContentItem;
import com.chen.blog.pojo.viewconfig.ViewOther;
import com.chen.blog.pojo.viewconfig.ViewTagCloud;
import com.chen.blog.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class ApplicationContextListener implements ApplicationListener<ApplicationReadyEvent> {
    
    @Autowired
    private AdminService adminService;
    
    @Autowired
    private ViewConfiguration configuration;
    
    @Autowired
    private ServletContext servletContext;
    
    
    /**
     * 初始化默认的配置信息
     *
     * @param contextPath
     * @return
     */
    private void initDefaultConfig(String contextPath) {
        ViewConfiguration rootConfig = configuration;
        rootConfig.setMainTitle("Blog");
        rootConfig.setMainHeadImage(contextPath + "/static/img/head.png");
        rootConfig.setShowFooter(true);
        
        ViewAboutStation viewAboutStation = new ViewAboutStation();
        viewAboutStation.setTitle("关于本站");
        viewAboutStation.setDetail("本站的详细内容");
        rootConfig.setViewAboutStation(viewAboutStation);
        
        ViewCarousel carousel = new ViewCarousel();
        List<ViewCarousel.OneImg> images = new ArrayList();
        images.add(new ViewCarousel.OneImg(contextPath + "/static/i/b1.jpg", "标题1", "内容1"));
        images.add(new ViewCarousel.OneImg(contextPath + "/static/i/b2.jpg", "标题2", "内容3"));
        images.add(new ViewCarousel.OneImg(contextPath + "/static/i/b3.jpg", "标题3", "内容3"));
        carousel.setViewCarousels(images);
        rootConfig.setViewCarousel(carousel);
        
        ViewContactMe viewContactMe = new ViewContactMe();
        viewContactMe.setGithub("#");
        viewContactMe.setOther("#");
        viewContactMe.setQq("#");
        viewContactMe.setWeibo("#");
        viewContactMe.setWeixin("#");
        rootConfig.setViewContactMe(viewContactMe);
        
        ViewContentItem viewContentItem = new ViewContentItem();
        ArrayList<String> itemHref = new ArrayList<>();
        for (int i = 0; i <= 14; i++) {
            itemHref.add(contextPath + "/static/img/img_" + i + ".jpg");
        }
        viewContentItem.setHref(itemHref);
        rootConfig.setViewContentItem(viewContentItem);
        
        ViewOther viewOther = new ViewOther();
        viewOther.setTitle("我的记录");
        viewOther.setItems(Collections.singletonList(new ViewOther.Item("Uychen博客系统", "#")));
        rootConfig.setViewOther(viewOther);
        
        ViewTagCloud viewTagCloud = new ViewTagCloud();
        viewTagCloud.setTagItems(Collections.singletonList(new ViewTagCloud.TagItem("Spring-Boot", "#")));
        rootConfig.setViewTagCloud(viewTagCloud);
        
        Setting setting = new Setting();
        setting.setId(Integer.MIN_VALUE);
        rootConfig.setSetting(setting);
    }
    
    
    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        ServletContext context = this.servletContext;
        ServerResponse<ViewConfiguration> active = adminService.findSettingByActive();
        initDefaultConfig(context.getContextPath());
        if (!active.isSuccess()) {
            configuration.getSetting().setStatus(true);
            context.setAttribute(Const.KEY_SETTINGS, configuration);
        } else {
            configuration.getSetting().setStatus(false);
            context.setAttribute(Const.KEY_SETTINGS, active.getData());
        }
    }
}
